package com.tjp.eight.ui.fragment.jifenshop;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.WApplication;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.views.rcview.EndlessRecyclerOnScrollListener;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

import java.util.Arrays;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by Administrator on 2017/3/13.
 */
public class FragmentJFShop extends BaseFragment<JFShopPresenter> implements JFShopView {
    private RecyclerView rv_jf_shop;
    private JFShopAdapter jfShopAdapter;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private GridLayoutManager gridLayoutManager;
    private BGABanner banner;
    private View headView;
    private TextView tvMoneyReset;
    View footView;

    private int page = 1;

    @Override
    protected JFShopPresenter createPresenter() {
        return new JFShopPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_jfshop, container, false);
        }
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPJson());
    }

    void initViews() {


        rv_jf_shop = (RecyclerView) contentView.findViewById(R.id.rv_jf_shop);
        gridLayoutManager = new GridLayoutManager(getContext(), 2);
        rv_jf_shop.setLayoutManager(gridLayoutManager);
        headView = LayoutInflater.from(getContext()).inflate(R.layout.item_jf_shop_head, rv_jf_shop, false);
        tvMoneyReset = (TextView) headView.findViewById(R.id.tv_jf_count);
        String money = getActivity().getIntent().getStringExtra("money");
        if (StringUtils.isNullOrEmpty(money)) {
            tvMoneyReset.setText("0");
        } else {
            tvMoneyReset.setText(money);
        }
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_jf_shop, false);
        jfShopAdapter = new JFShopAdapter(getContext());
        jfShopAdapter.setHeadView(headView);
        banner = (BGABanner) headView.findViewById(R.id.bb_jf_shop);
        banner.setAdapter(new BGABanner.Adapter<ImageView, String>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, String model, int position) {
                Glide.with(WApplication.getwApplication())
                        .load(model)
                        .placeholder(R.drawable.test_01)
                        .error(R.drawable.loading)
                        .centerCrop()
                        .dontAnimate()
                        .into(itemView);
            }
        });
        banner.setData(Arrays.asList("https://gss0.bdstatic.com/5eR1dDebRNRTm2_p8IuM_a/res/r/image/2017-02-13/e81f986adbf1bca604d65bd0d8ea272d.jpg"), null);
        banner.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                //Toast.makeText(banner.getContext(), "点击了" + position, Toast.LENGTH_SHORT).show();
            }
        });

        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(jfShopAdapter);
        rv_jf_shop.setAdapter(headerViewRecyclerAdapter);
        headerViewRecyclerAdapter.addFooterView(footView);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position == 0 && jfShopAdapter.getHeadView() != null ? 2 : 1;
            }
        });
        rv_jf_shop.addOnScrollListener(new EndlessRecyclerOnScrollListener(
                new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)
        ) {
            @Override
            public void onLoadMore(int currentPage) {
                page = currentPage;
                presenter.loadMData(getPJson());
            }
        });
    }

    private String getPJson() {
        JBShopPModel model = new JBShopPModel();
        model.page = page;
        return new Gson().toJson(model);
    }

    @Override
    public void loadMSuccess(JinBRModel model) {
        if (model != null && model.data != null) {
            jfShopAdapter.addData(model.data.list);
            if (jfShopAdapter.getItemCount() >= model.data.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        }
    }

    @Override
    public void loadSuccess(JinBRModel model) {
        if (model != null && model.data != null) {
            jfShopAdapter.addData(model.data.list);
            if (jfShopAdapter.getItemCount() >= model.data.total_items) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() == 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        }
    }

    @Override
    public void fail(String msg) {

    }
}
